package com.hy.mvc.biz.excel.exportstrategy;

import com.hy.mvc.biz.excel.model.SimpleExcelModel;
import com.hy.mvc.biz.utils.DateUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by Booker on 2017/7/3.
 */
@Component("SimpleTitleStrategy")
public class SimpleTitleStrategy implements TitleStrategy {

    private static final String DEFAULT_EXPORT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    @Override
    public HSSFSheet buildTitle(HSSFSheet workSheet, SimpleExcelModel excelModel) {
        // 设置报表标题字体
        HSSFFont fontTitle = workSheet.getWorkbook().createFont();
        fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        fontTitle.setFontHeight((short) 280);

        // 标题单元格样式
        HSSFCellStyle cellStyleTitle = workSheet.getWorkbook().createCellStyle();
        cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER);
        cellStyleTitle.setWrapText(true);
        cellStyleTitle.setFont(fontTitle);

        // 报表标题
        int startRowIndex = workSheet.getLastRowNum();
        int startColIndex = 0;

        HSSFRow rowTitle = workSheet.createRow((short) startRowIndex);
        rowTitle.setHeight((short) 500);
        HSSFCell cellTitle = rowTitle.createCell(startColIndex);
        cellTitle.setCellValue(excelModel.getTitle());
        cellTitle.setCellStyle(cellStyleTitle);

        // 合并区域内的报告标题
        workSheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
                excelModel.getHeader().length - 1));

        // date header
        HSSFRow dateTitle = workSheet.createRow((short) startRowIndex + 1);
        HSSFCell cellDate = dateTitle.createCell(startColIndex);
        cellDate.setCellValue("报表创建于:"
                + DateUtils.format(new Date(), DEFAULT_EXPORT_DATE_PATTERN));

        return workSheet;
    }
}
